import axios from "axios"
export default {
  namespaced: true,//开命名空间
  state: {
    goodsList: []
  },
  mutations: {//同步修改state的变量
    setGoodsList(state,list) {
      state.goodsList = list
    },
    setCheck(state, check) {
       state.goodsList.forEach(obj=>obj.goods_state=check)
    }
  },
  actions: {
    asyncGetGoodsList(store) {//异步setGoodsList
      axios({
        url: "https://www.escook.cn/api/cart",
      }).then((res) => {
       return store.commit("setGoodsList",res.data.list)
      });
    }
  },
  getters: {//类似和计算属性
    counts(state) {
      return state.goodsList
        .filter((obj) => obj.goods_state === true)
        .reduce((sum, obj) => {
          return (sum += obj.goods_count);
        }, 0);
    },
    sum(state) {
      return state.goodsList
        .filter((obj) => obj.goods_state === true)
        .reduce((sum, obj) => {
          return (sum += obj.goods_count * obj.goods_price);
        }, 0);
    },
    isCheck(state) {
      return state.goodsList.every((obj) => obj.goods_state);
    }
  }
}
 